{% extends "member/layout.html" %}

{% block title %}{{ project.name }}{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <!-- 页面标题和状态 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h3 class="fw-bold text-primary mb-1">
                    <i class="fas fa-folder-open me-2"></i>{{ project.name }}
                </h3>
                <p class="text-muted mb-0">{{ project.description }}</p>
            </div>
            <div class="d-flex align-items-center">
                <span class="badge status-badge bg-{{
                    'secondary' if project.status == 'draft'
                    else 'warning' if project.status == 'pending_review'
                    else 'danger' if project.status == 'rejected'
                    else 'dark' if project.status == 'cancelled'
                    else 'info' if project.status == 'approved'
                    else 'primary' if project.status == 'in_progress'
                    else 'success' if project.status == 'completed' or project.status == 'closed'
                    else 'info' if project.status == 'closing'
                    else 'success'
                }} bg-opacity-10 text-{{
                    'secondary' if project.status == 'draft'
                    else 'warning' if project.status == 'pending_review'
                    else 'danger' if project.status == 'rejected'
                    else 'dark' if project.status == 'cancelled'
                    else 'info' if project.status == 'approved'
                    else 'primary' if project.status == 'in_progress'
                    else 'success' if project.status == 'completed' or project.status == 'closed'
                    else 'info' if project.status == 'closing'
                    else 'success'
                }} border border-{{
                    'secondary' if project.status == 'draft'
                    else 'warning' if project.status == 'pending_review'
                    else 'danger' if project.status == 'rejected'
                    else 'dark' if project.status == 'cancelled'
                    else 'info' if project.status == 'approved'
                    else 'primary' if project.status == 'in_progress'
                    else 'success' if project.status == 'completed' or project.status == 'closed'
                    else 'info' if project.status == 'closing'
                    else 'success'
                }} border-opacity-25 rounded-pill px-3 py-2 me-3">
                    <i class="fas {{
                        'fa-edit' if project.status == 'draft'
                        else 'fa-clock' if project.status == 'pending_review'
                        else 'fa-times-circle' if project.status == 'rejected'
                        else 'fa-ban' if project.status == 'cancelled'
                        else 'fa-check-circle' if project.status == 'approved'
                        else 'fa-spinner' if project.status == 'in_progress'
                        else 'fa-flag-checkered' if project.status == 'completed'
                        else 'fa-file-signature' if project.status == 'closing'
                        else 'fa-check-square' if project.status == 'closed'
                        else 'fa-flag-checkered'
                    }} me-1"></i>
                    {{ project.status_label }}
                </span>
            </div>
        </div>

        <!-- 项目操作按钮组 - 修复和重构 -->
        <div class="d-flex flex-wrap align-items-center gap-2 mb-4">
            {% if current_member.id == project.owner_id %}
                {% if project.status == 'draft' or project.status == 'rejected' %}
                <a href="{{ url_for('member.jira.edit_project', project_id=project.id) }}" class="btn btn-warning btn-sm">
                    <i class="fas fa-edit me-1"></i> 编辑
                </a>
                <form method="post" action="{{ url_for('member.jira.submit_project', project_id=project.id) }}" class="d-inline" onsubmit="return confirm('确定要提交审核吗？');">
                    <input name="csrf_token" type="hidden" value="{{ csrf_token() }}">
                    <button type="submit" class="btn btn-primary btn-sm">
                        <i class="fas fa-paper-plane me-1"></i> 提交审核
                    </button>
                </form>
                {% endif %}

                {% if project.status in ['draft', 'rejected'] %}
                <form method="post" action="{{ url_for('member.jira.delete_project_route', project_id=project.id) }}" class="d-inline" onsubmit="return confirm('确定要删除此项目吗？此操作不可恢复。');">
                    <input name="csrf_token" type="hidden" value="{{ csrf_token() }}">
                    <button type="submit" class="btn btn-outline-danger btn-sm">
                        <i class="fas fa-trash me-1"></i> 删除
                    </button>
                </form>
                {% endif %}

                {% if project.status == 'approved' %}
                <form method="post" action="{{ url_for('member.jira.start_project', project_id=project.id) }}" class="d-inline">
                    <input name="csrf_token" type="hidden" value="{{ csrf_token() }}">
                    <button type="submit" class="btn btn-success btn-sm">
                        <i class="fas fa-play-circle me-1"></i> 开始项目
                    </button>
                </form>
                {% endif %}

                {% if project.status == 'in_progress' %}
                <button onclick="requestProjectClosure({{ project.id }})"
                        class="btn btn-primary btn-sm">
                    <i class="fas fa-flag-checkered me-1"></i> 完成项目
                </button>
                {% endif %}
            {% endif %}

            {# 修复和优化：仅当用户对当前项目有待审批时，才显示处理按钮 #}
            {% if pending_approval_id %}
            <a href="{{ url_for('member.approval.review_detail', approval_id=pending_approval_id) }}" class="btn btn-info btn-sm">
                <i class="fas fa-user-clock me-1"></i> 处理我的审批
            </a>
            {% endif %}
        </div>

        <div class="row">
            <!-- 左侧内容区域 -->
            <div class="col-lg-8">
                <!-- 项目概览卡片 -->
                <div class="card dashboard-card border-0 mb-4">
                    <div class="card-header bg-transparent border-0 pb-0">
                        <ul class="nav nav-tabs nav-tabs-custom border-0" role="tablist">
                            <li class="nav-item" role="presentation">
                                <button class="nav-link active" data-bs-toggle="tab" data-bs-target="#overview" type="button">
                                    <i class="fas fa-chart-pie me-2"></i>项目概览
                                </button>
                            </li>
                            <li class="nav-item" role="presentation">
                                <button class="nav-link" data-bs-toggle="tab" data-bs-target="#stages" type="button">
                                    <i class="fas fa-layer-group me-2"></i>项目阶段
                                </button>
                            </li>
                                <li class="nav-item" role="presentation">
                                    <button class="nav-link" data-bs-toggle="tab" data-bs-target="#approval" type="button">
                                        <i class="fas fa-clipboard-check me-2"></i>审批流程
                                    </button>
                                </li>
                                {# 添加结项评审标签页 #}
                                {% if project.status == 'closed' %}
                                <li class="nav-item" role="presentation">
                                    <button class="nav-link" data-bs-toggle="tab" data-bs-target="#closure-review" type="button">
                                        <i class="fas fa-file-signature me-2"></i>结项评审
                                    </button>
                                </li>
                                {% endif %}
                            </ul>
                        </div>
                        <div class="card-body">
                            <div class="tab-content">
                            <!-- 概览标签页 -->
                            <div class="tab-pane fade show active" id="overview">
                                <div class="row">
                                    <!-- 项目统计 -->
                                    <div class="col-md-6 mb-4">
                                        <h6 class="fw-bold text-muted mb-3">项目统计</h6>
                                        <div class="row text-center">
                                            <div class="col-4">
                                                <div class="border-end">
                                                    <h3 class="fw-bold text-primary mb-1">
                                                        {% if project.stages %}{% set total = namespace(value=0) %}{% for stage in project.stages %}{% set total.value = total.value + stage.tasks|length %}{% endfor %}{{ total.value }}{% else %}0{% endif %}
                                                    </h3>
                                                    <small class="text-muted">总任务</small>
                                                </div>
                                            </div>
                                            <div class="col-4">
                                                <div class="border-end">
                                                    <h3 class="fw-bold text-success mb-1">
                                                        {{ project.completed_tasks }}
                                                    </h3>
                                                    <small class="text-muted">已完成</small>
                                                </div>
                                            </div>
                                            <div class="col-4">
                                                <h3 class="fw-bold text-info mb-1">
                                                    {{ project.members|length if project.members else 1 }}
                                                </h3>
                                                <small class="text-muted">成员</small>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- 项目时间线 - 已移除 -->
                                </div>

                                <!-- 项目详情 -->
                                <div class="row">
                                    <div class="col-md-6">
                                        <h6 class="fw-bold text-muted mb-3">基本信息</h6>
                                        <div class="info-list">
                                            <div class="info-item d-flex justify-content-between py-2 border-bottom">
                                                <span class="text-muted">负责人</span>
                                                <span class="fw-medium">{{ project.owner.name }}</span>
                                            </div>
                                            <div class="info-item d-flex justify-content-between py-2 border-bottom">
                                                <span class="text-muted">开始日期</span>
                                                <span class="fw-medium">{{ project.started_at.strftime('%Y-%m-%d') if project.started_at else '未设置' }}</span>
                                            </div>
                                            <div class="info-item d-flex justify-content-between py-2 border-bottom">
                                                <span class="text-muted">结束日期</span>
                                                <span class="fw-medium">{{ project.end_date.strftime('%Y-%m-%d') if project.end_date else '未设置' }}</span>
                                            </div>
                                            <div class="info-item d-flex justify-content-between py-2 border-bottom">
                                                <span class="text-muted">类别</span>
                                                <span class="fw-medium">{{ project.category.name if project.category else '未设置' }}</span>
                                            </div>
                                            <div class="info-item d-flex justify-content-between py-2 border-bottom">
                                                <span class="text-muted">第一审批人</span>
                                                <span class="fw-medium">{{ project.first_approver.name if project.first_approver else '未设置' }}</span>
                                            </div>
                                            <div class="info-item d-flex justify-content-between py-2">
                                                <span class="text-muted">项目级别</span>
                                                <span class="badge" style="background-color: {{ project.level.color if project.level else '#6c757d' }}">
                                                    {{ project.level.name if project.level else '未设置' }}
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <h6 class="fw-bold text-muted mb-3">进度概览</h6>
                                        <div class="progress mb-3" style="height: 10px;">
                                            <div class="progress-bar bg-success" role="progressbar"
                                                 style="width: {{ project.progress }}%"
                                                 aria-valuenow="{{ project.progress }}"
                                                 aria-valuemin="0"
                                                 aria-valuemax="100"></div>
                                        </div>
                                        <div class="text-center">
                                            <h4 class="fw-bold text-primary">{{ "%.1f"|format(project.progress) }}%</h4>
                                            <p class="text-muted small">已完成 {{ project.completed_tasks }}/{{ project.total_tasks }} 个任务</p>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- 阶段标签页 -->
                            <div class="tab-pane fade" id="stages">
                                <div class="stages-accordion" id="stagesAccordion">
                                    {% for stage in project.stages %}
                                    <div class="card stage-card border-0 mb-3">
                                        <div class="card-header bg-transparent border-0 p-0">
                                            <button class="btn btn-toggle w-100 text-start p-3 rounded {% if not loop.first %}collapsed{% endif %}"
                                                    type="button" data-bs-toggle="collapse"
                                                    data-bs-target="#stage{{ stage.id }}"
                                                    aria-expanded="{% if loop.first %}true{% else %}false{% endif %}">
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <div>
                                                        <h6 class="fw-bold mb-1">{{ stage.name }}</h6>
                                                        <p class="text-muted small mb-0">{{ stage.description }}</p>
                                                    </div>
                                                    <div class="d-flex align-items-center">
                                                        <span class="badge bg-primary bg-opacity-10 text-primary rounded-pill me-3">
                                                            {{ stage.tasks|length }} 个任务
                                                        </span>
                                                        <i class="fas fa-chevron-down transition-rotate"></i>
                                                    </div>
                                                </div>
                                            </button>
                                        </div>
                                        <div id="stage{{ stage.id }}"
                                             class="collapse {% if loop.first %}show{% endif %}"
                                             data-bs-parent="#stagesAccordion">
                                            <div class="card-body pt-0">
                                                <div class="table-responsive">
                                                    <table class="table table-hover align-middle">
                                                        <thead class="table-light">
                                                            <tr>
                                                                <th>任务名称</th>
                                                                <th>负责人</th>
                                                                <th>状态</th>
                                                                <th>截止日期</th>
                                                                <th class="text-end">操作</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            {% for task in stage.tasks %}
                                                            <tr>
                                                                <td>
                                                                    <div class="d-flex align-items-center">
                                                                        <i class="fas fa-tasks text-muted me-2"></i>
                                                                        <span class="fw-medium">{{ task.name }}</span>
                                                                    </div>
                                                                </td>
                                                                <td>
                                                                    <span class="badge bg-light text-dark">
                                                                        {{ task.assignee.name if task.assignee else '未分配' }}
                                                                    </span>
                                                                </td>
                                                                <td>
                                                                    <span class="badge status-badge bg-{{
                                                                        'warning' if task.status == 'pending'
                                                                        else 'info' if task.status == 'in_progress'
                                                                        else 'success'
                                                                    }} bg-opacity-10 text-{{
                                                                        'warning' if task.status == 'pending'
                                                                        else 'info' if task.status == 'in_progress'
                                                                        else 'success'
                                                                    }} border border-{{
                                                                        'warning' if task.status == 'pending'
                                                                        else 'info' if task.status == 'in_progress'
                                                                        else 'success'
                                                                    }} border-opacity-25 rounded-pill px-2">
                                                                        {{ task.status_label }}
                                                                    </span>
                                                                </td>
                                                                <td>
                                                                    {% if task.due_date %}
                                                                    <small class="text-muted">{{ task.due_date.strftime('%Y-%m-%d') }}</small>
                                                                    {% else %}
                                                                    <small class="text-muted">未设置</small>
                                                                    {% endif %}
                                                                </td>
                                                                <td class="text-end">
                                                                    <a href="{{ url_for('member.task.task_detail', task_id=task.id) }}" class="btn btn-sm btn-outline-primary">
                                                                        <i class="fas fa-eye me-1"></i>详情
                                                                    </a>
                                                                </td>
                                                            </tr>
                                                            {% else %}
                                                            <tr>
                                                                <td colspan="5" class="text-center py-4 text-muted">
                                                                    <i class="fas fa-tasks me-2"></i>暂无任务
                                                                </td>
                                                            </tr>
                                                            {% endfor %}
                                                        </tbody>
                                                    </table>
                                                </div>

                                                {% if project.status == 'in_progress' and current_member.id == project.owner_id %}
                                                <div class="text-end mt-3">
                                                    <a href="{{ url_for('member.task.create', stage_id=stage.id) }}"
                                                       class="btn btn-primary btn-sm">
                                                        <i class="fas fa-plus me-1"></i>添加任务
                                                    </a>
                                                </div>
                                                {% endif %}
                                            </div>
                                        </div>
                                    </div>
                                    {% else %}
                                    <div class="text-center py-5">
                                        <i class="fas fa-layer-group text-muted mb-3" style="font-size: 3rem;"></i>
                                        <h5 class="text-muted">暂无阶段</h5>
                                        <p class="text-muted">项目尚未设置任何阶段</p>
                                    </div>
                                    {% endfor %}
                                </div>
                            </div>

                                <!-- 审批流程标签页 -->
                                <div class="tab-pane fade" id="approval">
                                    {% if project.approval_flow %}
                                    <div class="approval-flow-container">
                                        <h6 class="fw-bold text-muted mb-3">审批流程</h6>
                                        <div class="d-flex flex-wrap gap-2 mb-4">
                                            {% if project.first_approver %}
                                            <span class="badge fs-6 bg-primary bg-opacity-10 text-primary border border-primary border-opacity-25">
                                                <i class="fas fa-user-check me-1"></i>
                                                {{ project.first_approver.name }} (第一审批人)
                                            </span>
                                            {% endif %}
                                            {% for step in project.approval_flow.steps|sort(attribute='order') %}
                                            <span class="badge fs-6 bg-secondary bg-opacity-10 text-secondary border border-secondary border-opacity-25">
                                                <i class="fas fa-shoe-prints me-1"></i>
                                                {{ step.user.name }} (步骤 {{ step.order }})
                                            </span>
                                            {% endfor %}
                                        </div>

                                        <h6 class="fw-bold text-muted mb-3">审批历史</h6>
                                        {% if approval_history %}
                                        <div class="timeline">
                                            {% for record in approval_history %}
                                                <div class="timeline-item">
                                                    <div class="timeline-marker bg-{{ 'success' if record.status == 'approved' else 'danger' if record.status == 'rejected' else 'secondary' }}">
                                                        <i class="fas {{ 'fa-check' if record.status == 'approved' else 'fa-times' if record.status == 'rejected' else 'fa-hourglass-half' }}"></i>
                                                    </div>
                                                    <div class="timeline-content">
                                                        <p class="fw-bold mb-1">
                                                            {% if record.step_order == 0 %}
                                                            第一审批人: {{ record.reviewer.name if record.reviewer else '未知审批人' }}
                                                            {% else %}
                                                            步骤 {{ record.step_order }}: {{ record.reviewer.name if record.reviewer else '未知审批人' }}
                                                            {% endif %}
                                                            <span class="badge float-end bg-{{ 'success' if record.status == 'approved' else 'danger' if record.status == 'rejected' else 'warning' }}">{{ record.status_label }}</span>
                                                        </p>
                                                        {% if record.notes %}
                                                        <p class="text-muted mb-1 fst-italic">"{{ record.notes }}"</p>
                                                        {% endif %}
                                                        <small class="text-muted">
                                                            {% if record.reviewed_at %}
                                                            {{ record.reviewed_at.strftime('%Y-%m-%d %H:%M') }}
                                                            {% else %}
                                                            等待处理
                                                            {% endif %}
                                                        </small>
                                                    </div>
                                                </div>
                                            {% endfor %}
                                        </div>
                                        {% else %}
                                        <div class="text-center py-4 text-muted">
                                            <i class="fas fa-history me-2"></i>暂无审批历史记录
                                        </div>
                                        {% endif %}
                                    </div>
                                    {% else %}
                                    <div class="text-center py-5">
                                        <i class="fas fa-clipboard-check text-muted mb-3" style="font-size: 3rem;"></i>
                                        <h5 class="text-muted">暂无审批流程</h5>
                                        <p class="text-muted">项目尚未设置审批流程</p>
                                    </div>
                                    {% endif %}
                                </div>

                                {# 结项评审标签页 #}
                                <div class="tab-pane fade" id="closure-review">
                                    <h5 class="mb-4">项目结项评审</h5>
                                    <div class="list-group list-group-flush">
                                        <div class="list-group-item">
                                            <h6 class="fw-medium">结项申请信息</h6>
                                            <div class="text-muted small">
                                                申请时间：{{ project.closure_reviewed_at.strftime('%Y-%m-%d %H:%M') }}
                                            </div>
                                            <div class="text-muted small mt-1">
                                                申请人：{{ project.owner.name}}
                                            </div>
                                        </div>
                                        <div class="list-group-item">
                                            <h6 class="fw-medium">评审记录</h6>
                                            <div id="closure-reviews-container">
                                                <!-- 评审记录将通过AJAX加载 -->
                                                <div class="text-center py-4">
                                                    <div class="spinner-border text-primary" role="status">
                                                        <span class="visually-hidden">加载中...</span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            <!-- 右侧边栏 -->
            <div class="col-lg-4">
                <!-- 项目成员卡片 -->
                <div class="card dashboard-card border-0">
                    <div class="card-header bg-transparent border-0">
                        <h5 class="fw-bold mb-0">
                            <i class="fas fa-users me-2"></i>项目成员
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="members-list">
                            {% for member in project.members %}
                            <div class="member-item d-flex align-items-center py-2 {% if not loop.last %}border-bottom{% endif %}">
                                <div class="flex-shrink-0">
                                    <div class="avatar-sm bg-primary bg-opacity-10 text-primary rounded-circle d-flex align-items-center justify-content-center">
                                        <i class="fas fa-user"></i>
                                    </div>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h6 class="mb-0 fw-medium">{{ member.member.name }}</h6>
                                    <small class="text-muted">项目成员</small>
                                </div>
                                <div class="flex-shrink-0">
                                    <span class="badge bg-primary rounded-pill">成员</span>
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.status-badge {
    font-weight: 500;
    font-size: 0.9rem;
}

.nav-tabs-custom .nav-link {
    border: none;
    border-radius: 8px;
    margin-right: 8px;
    padding: 0.75rem 1.25rem;
    color: #6c757d;
    font-weight: 500;
    transition: all 0.2s;
}

.nav-tabs-custom .nav-link.active {
    background-color: rgba(67, 97, 238, 0.1);
    color: var(--primary-color);
}

.nav-tabs-custom .nav-link:hover:not(.active) {
    background-color: rgba(108, 117, 125, 0.1);
}

.btn-toggle {
    background-color: #f8f9fa;
    border: 1px solid #e9ecef;
    transition: all 0.2s;
}

.btn-toggle:hover {
    background-color: #e9ecef;
}

.btn-toggle[aria-expanded="true"] .fa-chevron-down {
    transform: rotate(180deg);
}

.transition-rotate {
    transition: transform 0.2s ease-in-out;
}

.stage-card .card-body {
    border-top: 1px solid #e9ecef;
}

.avatar-sm {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.dropdown-toggle::after {
    display: none;
}

.btn-light.rounded-circle {
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

/* Timeline styles */
.timeline {
    position: relative;
    padding-left: 30px;
    border-left: 2px solid #e9ecef;
}
.timeline-item {
    position: relative;
    margin-bottom: 20px;
}
.timeline-item:last-child {
    margin-bottom: 0;
}
.timeline-marker {
    position: absolute;
    top: 0;
    left: -40px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 10px;
}
.timeline-content {
    padding-left: 15px;
}
</style>

<script>
function requestProjectClosure(projectId) {
    console.log('Requesting project closure for:', projectId);
    if (confirm('确定要申请结项此项目吗？')) {
        const csrfToken = document.querySelector('input[name="csrf_token"]')?.value || '{{ csrf_token() }}';
        console.log('Using CSRF token:', csrfToken);

        fetch(`/member/jira/${projectId}/request-closure`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrfToken,
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: JSON.stringify({})
        })
        .then(response => {
            if (!response.ok) {
                return response.json().then(err => {
                    throw new Error(err.message || err.error || '请求失败');
                });
            }
            return response.json();
        })
        .then(data => {
            alert(data.message || '结项申请已提交');
            location.reload();
        })
        .catch(error => {
            console.error('Error:', error);
            alert(error.message || '请求失败，请检查网络连接');
        });
    }
}

document.addEventListener('DOMContentLoaded', function() {
    // 处理阶段折叠面板的旋转图标
    const stageButtons = document.querySelectorAll('.btn-toggle');
    stageButtons.forEach(button => {
        button.addEventListener('click', function() {
            const icon = this.querySelector('.fa-chevron-down');
            if (icon) {
                icon.classList.toggle('rotate-180');
            }
        });
    });

    // 加载结项评审记录（如果存在结项评审标签页）
    const closureReviewTab = document.querySelector('#closure-review');
    if (closureReviewTab) {
        const observer = new IntersectionObserver((entries) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    loadClosureReviews();
                    observer.unobserve(entry.target);
                }
            });
        }, { threshold: 0.5 });

        observer.observe(closureReviewTab);
    }

function loadClosureReviews() {
    const container = document.querySelector('#closure-reviews-container');
    if (container) {
        const projectId = {{ project.id }};

        fetch(`/member/jira/${projectId}/closure-reviews`)
            .then(response => response.json())
            .then(reviews => {
                if (reviews.length === 0) {
                    container.innerHTML = `
                        <div class="text-center py-4 text-muted">
                            <i class="fas fa-file-alt me-2"></i>暂无评审记录
                        </div>
                    `;
                    return;
                }

                let html = '<div class="timeline">';
                reviews.forEach(review => {
                    html += `
                        <div class="timeline-item">
                            <div class="timeline-marker bg-${review.status === 'approved' ? 'success' : 'danger'}">
                                <i class="fas ${review.status === 'approved' ? 'fa-check' : 'fa-times'}"></i>
                            </div>
                            <div class="timeline-content">
                                <p class="fw-bold mb-1">
                                    ${review.reviewer || '未知评审人'}
                                    <span class="badge float-end bg-${review.status === 'approved' ? 'success' : 'danger'}">
                                        ${review.status === 'approved' ? '已批准' : '已拒绝'}
                                    </span>
                                </p>
                                ${review.review_notes ? `<p class="text-muted mb-1 fst-italic">"${review.review_notes}"</p>` : ''}
                                <small class="text-muted">
                                    ${review.reviewed_at ? new Date(review.reviewed_at).toLocaleString() : '等待处理'}
                                </small>
                                ${review.score ? `<small class="text-muted d-block">评分: ${review.score}/100</small>` : ''}
                            </div>
                        </div>
                    `;
                });
                html += '</div>';
                container.innerHTML = html;
            })
            .catch(error => {
                console.error('加载评审记录失败:', error);
                container.innerHTML = `
                    <div class="alert alert-danger">
                        <i class="fas fa-exclamation-triangle me-2"></i>加载评审记录失败
                    </div>
                `;
            });
    }
}

    // 附件上传功能初始化
    const uploadModalHTML = `
<div class="modal fade" id="uploadModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">上传项目附件</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="uploadForm" enctype="multipart/form-data">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <div class="mb-3">
                        <label for="fileInput" class="form-label">选择文件</label>
                        <input class="form-control" type="file" id="fileInput" name="file" required>
                    </div>
                    <div class="mb-3">
                        <label for="description" class="form-label">描述</label>
                        <textarea class="form-control" id="description" name="description" rows="3"></textarea>
                    </div>
                    <div class="progress mb-3 d-none" id="uploadProgress">
                        <div class="progress-bar" role="progressbar" style="width: 0%"></div>
                    </div>
                    <button type="submit" class="btn btn-primary">上传</button>
                </form>
            </div>
        </div>
    </div>
</div>`;

    // 将模态框添加到body末尾（如果不存在）
    if (!document.getElementById('uploadModal')) {
        document.body.insertAdjacentHTML('beforeend', uploadModalHTML);
    }

    // 初始化上传表单事件
    const uploadForm = document.getElementById('uploadForm');
    if (uploadForm) {
        uploadForm.addEventListener('submit', function(e) {
            e.preventDefault();
            const formData = new FormData(this);

            const uploadProgress = document.getElementById('uploadProgress');
            uploadProgress.classList.remove('d-none');

            // 这里应该是实际的AJAX上传代码
            // 示例代码，需要替换为真实的上传逻辑
            setTimeout(() => {
                uploadProgress.classList.add('d-none');
                const progressBar = uploadProgress.querySelector('.progress-bar');
                progressBar.style.width = '0%';

                // 模拟上传成功
                alert('附件上传成功');
                location.reload();
            }, 2000);
        });
    }
});
</script>
{% endblock %}
